use anyhow::Result;
use tonic::transport::Server;
use tracing::{info, level_filters::LevelFilter};
use tracing_subscriber::{fmt::Layer, layer::SubscriberExt, util::SubscriberInitExt, Layer as _};
use user_stat::UserStatsService;

#[tokio::main]
async fn main() -> Result<()> {
    let layer = Layer::new().with_filter(LevelFilter::INFO);
    tracing_subscriber::registry().with(layer).init();
    let service = UserStatsService::new().await;
    let port = service.config.server.port;
    let addr = format!("[::1]:{}", port).parse().unwrap();
    info!("listening on {}", addr);
    Server::builder()
        .add_service(service.into_server())
        .serve(addr)
        .await?;
    Ok(())
}
